package controller;

import entity.mvc.Token;
import entity.mvc.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import service.Result;
import service.TokenManageService;
import service.UserManageService;

/**
 * @program: SSM201759225309
 * @description:
 * @author: Xiaozhuoer
 * @create: 2021-05-07 13:14
 **/
@Controller
@RequestMapping("user")
public class UserController {

    private final UserManageService userManageService;
    private TokenManageService tokenManageService;

    public UserController(UserManageService userManageService){
        this.userManageService = userManageService;
        this.tokenManageService = new TokenManageService();
    }



    @RequestMapping("/logon")
    @ResponseBody
    public Result logonUser(User user){
        System.out.println("获取待注册用户信息: " + user);
        return userManageService.logonUser(user);
    }

    @RequestMapping("/login")
    @ResponseBody
    public Result loginUser(User user){
        System.out.println("获取待登录用户信息: " + user);
        Result result = new Result();
        int loginStatus = userManageService.loginUser(user).getStatus();
        if(loginStatus == 1){
            Token token = tokenManageService.CreateToken(0, user.getName(), user.getPassword());
            result.setStatus(1);
            result.setMsg("登录成功");
            result.setData(token.getUuid());
        }else{
            result.setStatus(1);
            result.setMsg("登录失败");
            result.setData(null);
        }
        return result;
    }

    @RequestMapping("/add")
    @ResponseBody
    public Result addUser(User user){
        System.out.println("获取待增用户信息: " + user);
        return userManageService.addUser(user);
    }

    @PostMapping("/find")
    @ResponseBody
    public Result findUser(User user){
        System.out.println("获取待查询用户信息: " + user);
        return userManageService.findUser(user);
    }

    @PostMapping("/update")
    @ResponseBody
    public Result updateUserInfo(User user){
        System.out.println("获取待更新用户信息: " + user);
        return userManageService.updateUserInfo(user);
    }

    @PostMapping("/delete")
    @ResponseBody
    public Result deleteUser(User user){
        System.out.println("获取待删除用户信息: " + user);
        return userManageService.deleteUser(user);
    }
}
